package view;

import com.alee.extended.painter.BorderPainter;
import com.alee.extended.panel.WebOverlay;
import com.alee.laf.label.WebLabel;
import com.alee.laf.panel.WebPanel;
import com.alee.laf.text.WebTextArea;
import com.alee.utils.swing.ConditionalVisibilityListener;
import controller.App;
import controller.Post;
import controller.Query;
import model.PostModel;
import util.FontLoader;

import javax.swing.BoxLayout;
import javax.swing.SwingConstants;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Date;

public class PostList extends WebPanel {

    public String targetID;
    public WebLabel inputLabel;

    public PostList (String targetID) {
        this.targetID = targetID;
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.setOpaque(false);
        this.setAlignmentX(Component.CENTER_ALIGNMENT);

        this.createPostInput();

        showPosts(Query.getPosts(targetID));
    }

    public PostList (ArrayList<Post> posts) {
        this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        this.setOpaque(false);
        this.setAlignmentX(Component.CENTER_ALIGNMENT);

        this.createPostInput();
        this.showPosts(posts);
    }

    /**
     * Show the posts for this screen
     */
    public void showPosts(ArrayList<Post> posts) {
        for (Post p : posts) {
            this.add(p.view);
        }
    }

    /**
     * Create the post input field
     */
    public void createPostInput() {
        WebPanel panel = new WebPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setOpaque(false);
        panel.setMargin(10, 0, 40, 0);
        final WebTextArea textField = new WebTextArea("");
        BorderPainter bp2 = new BorderPainter ();

        bp2.setMargin(6);
        bp2.setRound ( 4 );
        bp2.setWidth ( 1 );
        bp2.setColor ( new Color( 200, 200, 200 ) );
        textField.setPainter ( bp2 );
        final WebPanel self = this;

        textField.addKeyListener(new KeyListener() {

            @Override
            public void keyPressed(KeyEvent arg0) {
            }

            @Override
            public void keyReleased(KeyEvent arg0) {
                if (!arg0.isShiftDown() && arg0.getKeyCode() == KeyEvent.VK_ENTER && textField.getText().trim().length() > 0) {
                    PostModel m = new PostModel();
                    m.authorName = App.getCurrentUser().name;
                    m.authorId = App.getCurrentUser().userId;
                    m.targetId = targetID;
                    m.text = textField.getText();
                    m.date = new Date();
                    m.commit();
                    self.add(new Post(m).view, 1);
                    textField.setText("");
                }
            }

            @Override
            public void keyTyped(KeyEvent arg0) {
            }

        });

        Font osRegular = FontLoader.getFont(FontLoader.OPENSANS_REGULAR);
        textField.setFont(osRegular.deriveFont(14f));
        textField.setAlignmentX(Component.LEFT_ALIGNMENT);
        textField.setWrapStyleWord(true);
        textField.setLineWrap(true);
        textField.setForeground(new Color(100, 100, 100));

        // Overlayed text field
        WebOverlay overlayPanel = new WebOverlay();
        overlayPanel.setComponent(textField);

        // Label displayed as overlay when component is not focused
        @SuppressWarnings("serial")
        final WebLabel overlay = inputLabel = new WebLabel("What's on your mind?") {
            public boolean contains(int x, int y) {
                return false;
            }
        };

        overlay.setFont(osRegular.deriveFont(14f));
        overlay.setForeground(Color.GRAY);
        overlay.setMargin(textField.getInsets());

        textField.addFocusListener(new ConditionalVisibilityListener(overlay, false, null) {
            public boolean isVisible() {
                return super.isVisible() && textField.getText().equals("");
            }
        });

        textField.getDocument().putProperty("filterNewlines", Boolean.TRUE);

        overlayPanel.addOverlay(overlay, SwingConstants.LEFT, SwingConstants.CENTER);
        overlayPanel.setOpaque(false);
        overlayPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
        panel.add(overlayPanel);

        this.add(panel, 0);

    }
}
